package com.minhua.jijiehao2.mainframe.launcher.finalregisteractivity.impl

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.mainframe.launcher.finalregisteractivity.imp.IFinalRegisterView

import android.view.View
import com.minhua.jijiehao2.mainframe.main.impl.MainActivity
import com.minhua.jijiehao2.utils.T
import kotlinx.android.synthetic.main.activity_final_register.*

class FinalRegisterActivity : BaseActivity<IFinalRegisterView, FinalRegisterPresenter>(), IFinalRegisterView {

    override fun getLayoutId(): Int = R.layout.activity_final_register

    override fun getPresenter(): FinalRegisterPresenter = FinalRegisterPresenter()

    companion object {

        fun intentStart(context: Context, phone: String, code: String) {
            val intent = Intent(context, FinalRegisterActivity::class.java)
            intent.putExtra("phone", phone)
            intent.putExtra("code", code)
            context.startActivity(intent)
        }
    }

    private val mOnClickListener = View.OnClickListener { v ->
        when (v.id) {
            R.id.ivBack -> finish()
            R.id.tvSure -> mPresenter.doRegister()
        }
    }

    override fun initView() {
        password.editText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                tvSure.isEnabled = !TextUtils.isEmpty(password.text)
            }
        })
        ivBack.setOnClickListener(mOnClickListener)
        tvSure.setOnClickListener(mOnClickListener)
    }

    override fun getPhone(): String = intent.getStringExtra("phone")

    override fun getCode(): String = intent.getStringExtra("code")

    override fun getPassword() = password.text!!

    override fun onRegisterSuccess() {
        T.showShort("注册成功")
        Handler().postDelayed({
            startActivity(Intent(this, MainActivity::class.java))
        }, 500)
    }


}
